package com.zetyun.tiger.sql.meta.col;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;
import com.zetyun.tiger.sql.meta.tn.GenOdsTableApp;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

public class GenOdsColumnApp {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("insert into ods_column values \n");

        String tbName = "PB_FUND_TRANFLOW";

        String insertTemplate = "('" + tbName + "','{}','{}','{}','{}','{}','{}','{}'),\n";

        List<String> tCols = FileUtil.readLines(Objects.requireNonNull(
                GenOdsColumnApp.class.getResource("/ods_column.txt")), StandardCharsets.UTF_8);

        for (String tCol : tCols) {
            if (tCol.startsWith("字段英文名")) {
                continue;
            }
            String[] arr = tCol.split("\t");

            String field_description = "";
            if (arr.length>6) {
                field_description = arr[6].trim();
            }
            String column_name = arr[0].trim();
            String column_cn_name = arr[1].trim();
            String column_type = arr[2].trim();
            if (column_type.startsWith("VARCHAR")) {
                column_type = column_type.replace("VARCHAR2", "VARCHAR");
            }
            if (column_type.startsWith("CHAR(")) {
                column_type = column_type.replace("CHAR", "VARCHAR");
            }
            if (column_type.startsWith("CHARACTER")) {
                column_type = column_type.replace("CHARACTER", "VARCHAR");
            }
            if (column_type.startsWith("SMALLINT")) {
                column_type = "SMALLINT";
            }

            if (column_type.startsWith("NUMBER")) {
                column_type = column_type.replace("NUMBER", "DECIMAL");
                if (column_type.split(",").length==1) {
                    column_type = column_type.replace(")", ",0)");
                }
            }

            if (column_type.startsWith("TIMESTAMP")) {
                column_type = "TIMESTAMP_LTZ";
            }
            String column_serial = arr[3].trim();
            String nullable = arr[4].trim().equals("是") ? "1" : "0";
            String is_primary_key = arr[5].trim().equals("是") ? "1" : "0";
            String item = StrFormatter.format(insertTemplate,
                    column_serial,
                    column_name,
                    column_cn_name,
                    column_type,
                    is_primary_key,
                    nullable,
                    field_description
                    );
            sb.append(item);
        }

        System.out.println(sb);
    }
}
